package com.leoman.plz_p.presenter;

import com.leoman.plz_p.interfaces.ClassifyDetailView;
import com.leoman.plz_p.module.api.MainApi;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.CourseBean;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.subscriber.ResponseSubscriber;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

/**
 * Created by guoh on 2018/10/16.
 */
public class ClassifyDetailPresenrer extends BasePresenter<ClassifyDetailView> {

    private MainApi mainApi;
    private int pageNo;

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        mainApi=getApi(MainApi.class);
    }

    @Override
    public void onStart() {

    }

    public void getCourseByCategory(String id,String name, final boolean reload){
        if (reload)pageNo=1;else pageNo+=1;
        mainApi.getCourseByCategory(pageNo,12,id,name).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CourseBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CourseBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CourseBean>> listData) {
                        if (checkListNotNull(listData.data.getRows())){
                            view.obtainCourseList(listData.data.getRows(),reload);
                        }else {
                            if (reload){
                                view.obtainNoData();
                            }else {
                                view.obtainNoMoreData();
                            }
                        }
                    }

                });
    }
}
